using Furion.DatabaseAccessor;

using System.Threading.Tasks;

using Tool;

namespace Abc.Application
{
    /// <summary>
    /// 赠删改查父类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TDbContextLocator"></typeparam>
    public abstract class CurdService<TEntity, TDbContextLocator>
        where TEntity : class, IPrivateEntity, new()
        where TDbContextLocator : class, IDbContextLocator
    {
        /// <summary>
        /// 注入仓储
        /// </summary>
        private readonly IRepository<TEntity, TDbContextLocator> _repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public CurdService()
        {
            _repository = Db.GetRepository<TEntity, TDbContextLocator>();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TEntity> Insert(TEntity entity)
        {
            var entryEntity = await _repository.InsertAsync(entity);
            return entryEntity.Entity;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> Update(TEntity entity)
        {
            var resultdata = new ResultData<string>();
            var entryEntity = await _repository.UpdateAsync(entity);
            resultdata.Succeeded = true;
            resultdata.Errors = "成功";
            return resultdata;
        }
    }
}